package cn.iocoder.yudao.module.pi.controller.admin.pipline.task;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pi.controller.admin.pipline.task.vo.*;
import cn.iocoder.yudao.module.pi.dal.dataobject.task.TaskDO;
import cn.iocoder.yudao.module.pi.service.task.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流水线任务")
@RestController
@RequestMapping("/pi/pipline/task")
@Slf4j
public class PiplineTaskController {

    @Resource
    private TaskService taskService;
    @PostMapping("/create")
    @Operation(summary = "创建流水线任务")
    //@PreAuthorize("@ss.hasPermission('pipline:task:create')")
    @PermitAll
    @TenantIgnore
    public CommonResult<Long> createTask(@Valid @RequestBody TaskCreateReqVO taskCreateReqVO) {
        Long taskId=0L;
        taskCreateReqVO.getStages().forEach(stage -> {
            stage.getJobs().forEach(job -> {
                TaskSaveReqVO createReqVO=new TaskSaveReqVO();
                createReqVO.setRegion(taskCreateReqVO.getRegion());
                createReqVO.setRegion(taskCreateReqVO.getRegion());
                createReqVO.setProjectId(taskCreateReqVO.getProjectId());
                createReqVO.setPiplineId(taskCreateReqVO.getPipelineId());
                createReqVO.setPiplineName(taskCreateReqVO.getName());
                createReqVO.setStageCiId(stage.getIdentifier());
                createReqVO.setStageCiName(stage.getName());
                createReqVO.setTaskCiId(job.getIdentifier());
                createReqVO.setTaskCiName(job.getName());
                createReqVO.setEnv(taskCreateReqVO.getEnv());
                createReqVO.setDeployType(taskCreateReqVO.getDeployType());
                taskService.createTask(createReqVO);
            });
        });
        return success(taskId);
    }

    @PostMapping("/getStageTask/{deployType}")
    @Operation(summary = "获得流水线阶段任务")
    //@PreAuthorize("@ss.hasPermission('pipline:task:getStageTask')")
    @PermitAll
    @TenantIgnore
    public CommonResult<List<StageTaskRespVO>> getStageTask(@PathVariable String deployType) {
        StageTaskReqVO stageTaskReqVO=new StageTaskReqVO();
        stageTaskReqVO.setEnv("sit");
        stageTaskReqVO.setDeployType(deployType);
        List<StageTaskRespVO> list=new ArrayList<>();
        List<TaskDO> taskDOList=taskService.getStageTask(stageTaskReqVO);
        Map<String,StageTaskRespVO> stageMap=new HashMap<>();
        taskDOList.forEach(taskDO -> {
            if(!stageMap.containsKey(taskDO.getStageCiId())){
                StageTaskRespVO stageTaskRespVO=new StageTaskRespVO();
                stageTaskRespVO.setStageCiId(taskDO.getStageCiId());
                stageTaskRespVO.setStageCiName(taskDO.getStageCiName());
                stageTaskRespVO.setTasks(new ArrayList<TaskRespVO>());
                list.add(stageTaskRespVO);
                stageMap.put(taskDO.getStageCiId(),stageTaskRespVO);
            }
        });
        taskDOList.forEach(taskDO -> {
            StageTaskRespVO stageTaskRespVO=stageMap.get(taskDO.getStageCiId());
            TaskRespVO taskRespVO=new TaskRespVO();
            taskRespVO.setTaskCiId(taskDO.getTaskCiId());
            taskRespVO.setTaskCiName(taskDO.getTaskCiName());
            stageTaskRespVO.getTasks().add(taskRespVO);
        });
        return success(list);
    }

}
